package com.icomvn.game.pikachu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.icomvn.game.pikachu.config.Config;
import com.icomvn.game.pikachu.object.ActivityInterface;
import com.icomvn.game.pikachu.object.OnFocusChangeListenerButtonHigh;
import com.icomvn.game.pikachu.object.OnFocusChangeListenerButtonSelector;
import com.icomvn.game.pikachu.play.PlayPikachu;
import com.icomvn.game.pikachu.utility.Save;

public class Pikachu extends Activity implements ActivityInterface,
		OnClickListener {
	ImageView start, help, moreGame, music, exit, info, pi, exitpop, exitHelp, de, tbinh, kho;
	Activity activity;
	SharedPreferences setup;
	Editor editor;
	Animation leftTop;
	Animation leftBottom;
	Animation rigthTop;
	Animation rigthBottom;
	Button ok;
	Button cancel;
	EditText phone;
	ImageView pikachu, dongypop;
	ViewGroup alert1, alert2,alert3;
	TextView textpop;
	FrameLayout bgmenu, bghelp, bgdiff;
	int count = 0, Width, Height, key, diff;
	float raditoX, raditoY;
	boolean isConfirmMusic = false;
	int mode = 0;
	static boolean isRunning = false;
	final Context context = this;
	LinearLayout ln1, ln2;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.menu);

		Display display = ((WindowManager) getBaseContext().getSystemService(
				Context.WINDOW_SERVICE)).getDefaultDisplay();

		// lay chieu rong cua man hinh
		Width = display.getWidth();
		// lay chieu cao cua man hinh
		Height = display.getHeight();
		if (Width == 320 && Height == 240) {
			setContentView(R.layout.menu240320);
		}
		if (Width == 480 && Height == 320) {
			setContentView(R.layout.menu320480);
		}
		if (Width == 1280 && Height == 800) {
			setContentView(R.layout.menu8001280);
		}
		if (Width == 1280 && Height == 720) {
			setContentView(R.layout.menu7201280);
		}
		activity = this;
		setup = getSharedPreferences("pikachu_icom", Activity.MODE_PRIVATE);
		alert1 = (ViewGroup) findViewById(R.id.playalertmenu);
		alert2 = (ViewGroup) findViewById(R.id.playalerthelp);
		alert3 = (ViewGroup) findViewById(R.id.playalertmenudiff);
		textpop = (TextView) findViewById(R.id.text1);
		exitpop = (ImageView) findViewById(R.id.exit1);
		exitpop.setOnClickListener(this);
		exitHelp = (ImageView) findViewById(R.id.exit2);
		exitHelp.setOnClickListener(this);
		dongypop = (ImageView) findViewById(R.id.dongypop);
		dongypop.setOnClickListener(this);
		de = (ImageView) findViewById(R.id.de);
		de.setOnClickListener(this);
		tbinh = (ImageView) findViewById(R.id.tbinh);
		tbinh.setOnClickListener(this);
		kho = (ImageView) findViewById(R.id.kho);
		kho.setOnClickListener(this);
		bgmenu = (FrameLayout) findViewById(R.id.bgallmenu);
		bghelp = (FrameLayout) findViewById(R.id.bgallhelp);
		bgdiff = (FrameLayout) findViewById(R.id.bgdiff);
		editor = setup.edit();
		start = (ImageView) findViewById(R.id.choi);
		start.setOnClickListener(this);
		// Log.d("rong",Integer.toString(start.getWidth()) );
		info = (ImageView) findViewById(R.id.thongtin);
		info.setOnClickListener(this);
		moreGame = (ImageView) findViewById(R.id.themtrochoi);
		moreGame.setOnClickListener(this);
		music = (ImageView) findViewById(R.id.caidatam);
		music.setOnClickListener(this);
		help = (ImageView) findViewById(R.id.huongdan);
		help.setOnClickListener(this);
		exit = (ImageView) findViewById(R.id.thoat);
		exit.setOnClickListener(this);
		// lay chieu rong cua man hinh

		ln1 = (LinearLayout) findViewById(R.id.linearLayout1);
		ln2 = (LinearLayout) findViewById(R.id.linearLayout2);
		pikachu = (ImageView) findViewById(R.id.pikachu);
		pikachu.setBackgroundResource(R.drawable.pi1);
		// setupButtonListener();
		setupAnimation();
		saveSolution();
		Save.currentActivity = this;
		isRunning = true;

	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if (hasFocus) {
			animation(pikachu, R.drawable.pikachubg);
		}

	}

	private void animation(ImageView img, int ani) {
		if (img != null) {
			img.setVisibility(ImageView.VISIBLE);
			img.setBackgroundResource(ani);
			AnimationDrawable frameAnimation = (AnimationDrawable) img
					.getBackground();

			if (frameAnimation.isRunning()) {
				frameAnimation.stop();
			} else {
				frameAnimation.start();
			}
		}
	}

	public void saveSolution() {
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		int heigth = this.getWindowManager().getDefaultDisplay().getHeight();
		width = (width / 10) * 10
				+ (int) (Math.ceil((double) (width % 10) / 10) * 10);
		heigth = (heigth / 10) * 10
				+ (int) (Math.ceil((double) (heigth % 10) / 10) * 10);
		if (getResources().getConfiguration().orientation == 2) {
			Save.heigth = width;
			Save.width = heigth;
		} else {
			Save.width = width;
			Save.heigth = heigth;
		}
	}

	public void setupAnimation() {
		leftTop = AnimationUtils.loadAnimation(this, R.anim.left_top);
		leftBottom = AnimationUtils.loadAnimation(this, R.anim.left_bottom);
		rigthTop = AnimationUtils.loadAnimation(this, R.anim.rigth_top);
		rigthBottom = AnimationUtils.loadAnimation(this, R.anim.rigth_bottom);
		start.startAnimation(leftTop);
		help.startAnimation(rigthTop);
		music.startAnimation(leftBottom);
		info.startAnimation(rigthBottom);
		moreGame.startAnimation(leftTop);
		exit.startAnimation(rigthTop);
	}

	public void clickBG(Button v, boolean invi, boolean high) {
		final Button v1 = v;
		final boolean high1 = high;
		final boolean invi1 = invi;
		Animation clickButton = AnimationUtils.loadAnimation(this,
				R.anim.static_button);
		clickButton.setAnimationListener(new AnimationListener() {
			public void onAnimationEnd(Animation _animation) {
				if (high1)
					v1.setBackgroundResource(R.drawable.buttonhigh);
				else
					v1.setBackgroundResource(R.drawable.button);
				if (invi1) {
					start.setClickable(true);
					help.setClickable(true);
					info.setClickable(true);
					moreGame.setClickable(true);
					exit.setClickable(true);
					music.setClickable(true);
				}
			}

			public void onAnimationRepeat(Animation _animation) {
			}

			public void onAnimationStart(Animation _animation) {
				if (high1)
					v1.setBackgroundResource(R.drawable.buttonhighselected);
				else
					v1.setBackgroundResource(R.drawable.buttonselected);
			}
		});
		v1.startAnimation(clickButton);
	}
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			// TODO Auto-generated method stub
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				activity.finish();
				overridePendingTransition(R.anim.diagslide_enter_back,
						R.anim.diagslide_leave_back);
				return false;
			}
			// return super.onKeyDown(keyCode, event);
			return true;
		}

		public void setupButtonListener() {
			ok.setOnFocusChangeListener(new OnFocusChangeListenerButtonHigh());
			cancel.setOnFocusChangeListener(new OnFocusChangeListenerButtonHigh());
			start.setOnFocusChangeListener(new OnFocusChangeListenerButtonSelector());
			info.setOnFocusChangeListener(new OnFocusChangeListenerButtonSelector());
			music.setOnFocusChangeListener(new OnFocusChangeListenerButtonSelector());
			help.setOnFocusChangeListener(new OnFocusChangeListenerButtonSelector());
			moreGame.setOnFocusChangeListener(new OnFocusChangeListenerButtonSelector());
			exit.setOnFocusChangeListener(new OnFocusChangeListenerButtonSelector());
		}


	public void continueGame() {
		// TODO Auto-generated method stub

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.choi:
			lock();
			bgdiff.setBackgroundResource(R.drawable.bg_denmo);
			alert3.setVisibility(View.VISIBLE);
			overridePendingTransition(R.anim.diagslide_enter,
					R.anim.diagslide_leave);
			break;
		case R.id.caidatam:
			lock();
			alert("Tắt âm nhé???", 2);
			break;
		case R.id.huongdan:
			lock();
			help();
			break;
		case R.id.themtrochoi:
			final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(Config.gameURL));
			activity.startActivity(intent);
			overridePendingTransition(R.anim.diagslide_enter,
					R.anim.diagslide_leave);
			break;
		case R.id.thongtin:
			lock();
			alert("http://www.mbin.vn/", 1);
			break;
		case R.id.thoat:
			finish();
			overridePendingTransition(R.anim.diagslide_enter_back,
					R.anim.diagslide_leave_back);
			break;
		case R.id.dongypop:
			bgmenu.setBackgroundResource(R.drawable.tranparent);
			alert1.setVisibility(View.GONE);
			isConfirmMusic = true;
			unLock();
			break;
		case R.id.exit1:
			bgmenu.setBackgroundResource(R.drawable.tranparent);
			alert1.setVisibility(View.GONE);
			unLock();
			break;
		case R.id.exit2:
			bghelp.setBackgroundResource(R.drawable.tranparent);
			alert2.setVisibility(View.GONE);
			unLock();
			break;
		case R.id.de:
			bgdiff.setBackgroundResource(R.drawable.tranparent);
			alert3.setVisibility(View.GONE);
			diff=2;
			unLock();
			Intent mainIntent = new Intent(activity, PlayPikachu.class);
			mainIntent.putExtra("diff", diff);
			mainIntent.putExtra("mu", isConfirmMusic);
			activity.startActivity(mainIntent);
			overridePendingTransition(R.anim.diagslide_enter,
					R.anim.diagslide_leave);
			break;
		case R.id.tbinh:
			bgdiff.setBackgroundResource(R.drawable.tranparent);
			alert3.setVisibility(View.GONE);
			diff=1;
			unLock();
			Intent mainIntent1 = new Intent(activity, PlayPikachu.class);
			mainIntent1.putExtra("diff", diff);
			mainIntent1.putExtra("mu", isConfirmMusic);
			activity.startActivity(mainIntent1);
			overridePendingTransition(R.anim.diagslide_enter,
					R.anim.diagslide_leave);
			break;
		case R.id.kho:
			bgdiff.setBackgroundResource(R.drawable.tranparent);
			alert3.setVisibility(View.GONE);
			diff=0;
			unLock();
			Intent mainIntent2 = new Intent(activity, PlayPikachu.class);
			mainIntent2.putExtra("diff", diff);
			mainIntent2.putExtra("mu", isConfirmMusic);
			activity.startActivity(mainIntent2);
			overridePendingTransition(R.anim.diagslide_enter,
					R.anim.diagslide_leave);
			break;
		default:
			break;
		}
	}

	public void finish() {
		// TODO Auto-generated method stub
		isRunning = false;
		super.finish();
	}

	public void alertFinish() {
		// TODO Auto-generated method stub

	}

	public void alert(String message, int key) {
		this.key = key;
		lock();
		bgmenu.setBackgroundResource(R.drawable.bg_denmo);
		textpop.setText(message);
		if (key == 1) {
			dongypop.setVisibility(View.GONE);
		}
		alert1.setVisibility(View.VISIBLE);
		overridePendingTransition(R.anim.diagslide_enter,
				R.anim.diagslide_leave);
	}

	public void help() {
		lock();
		bghelp.setBackgroundResource(R.drawable.bg_denmo);
		alert2.setVisibility(View.VISIBLE);
		overridePendingTransition(R.anim.diagslide_enter,
				R.anim.diagslide_leave);
	}

	private void lock() {
		start.setClickable(false);
		help.setClickable(false);
		music.setClickable(false);
		info.setClickable(false);
		moreGame.setClickable(false);
		exit.setClickable(false);
	}

	private void unLock() {
		start.setClickable(true);
		help.setClickable(true);
		music.setClickable(true);
		info.setClickable(true);
		moreGame.setClickable(true);
		exit.setClickable(true);
	}
	// private void resize(){
	//
	//
	// ResizeImage.resizeImage(start,Height/8, Width/4, R.drawable.load_03);
	// ResizeImage.resizeImage(help,Height/8, Width/4, R.drawable.load_08);
	// ResizeImage.resizeImage(exit,Height/8, Width/4, R.drawable.load_10);
	//
	//
	// // ResizeImage.resizeImage(pikachu,2*Height/5, Width/3, R.drawable.pi1);
	//
	// ResizeImage.resizeImage(music, 2 * Height / 17, 2 * Width / 11,
	// R.drawable.load_06);
	// ResizeImage.resizeImage(moreGame, 2 * Height / 17, 2 * Width / 11,
	// R.drawable.load_09);
	//
	// ResizeImage.resizeImage(info, 2 * Height / 17, 2 * Width / 11,
	// R.drawable.load_11);
	//
	// RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
	// RelativeLayout.LayoutParams.WRAP_CONTENT,
	// RelativeLayout.LayoutParams.WRAP_CONTENT);
	// lp1.leftMargin = Width/8;
	// lp1.topMargin = 4*Height/9;
	// ln1.setLayoutParams(lp1);
	//
	// RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
	// RelativeLayout.LayoutParams.WRAP_CONTENT,
	// RelativeLayout.LayoutParams.WRAP_CONTENT);
	// lp2.leftMargin = 17*Width/24;
	// lp2.topMargin = 4*Height/9;
	// ln2.setLayoutParams(lp2);
	// }

}